// 手册加载
window.onload = function () {
    var url = 'http://192.168.0.112:8080/BannerManage/getAllBanner'
    function s(datas) {
        function operateFormatter(value, row, index) {//赋予的参数
            return [
                "<button  class='btn btn-primary' style='margin-right:10px' onclick=editad(" + row.bannerid + ",\'" + row.title + "\'," + row.info + "," + row.link + "," + row.bannertypeid + ")>编辑</button>",
                "<button class='btn btn-primary' style='margin-right:10px'  onclick=updateadImage(" + row.bannerid + ")>更新图片</button>"
            ].join('');
        }
        // 设置table列样式
        function formatFormatter(value, row, index) {
            if (value == 1) {

                return [

                    '<img src="../../../images/success.png" onclick="statusChange(' + row.validflag + ',' + row.bannerid + ')">',
                ].join('');
            } else {
                return [

                    '<img src="../../../images/fail.png" onclick="statusChange(' + row.validflag + ',' + row.bannerid + ')">',
                ].join('');
            }
        }
        // 设置图片
        function operateFormatterimage(value, row, index) {
            return [
                '<img src="' + row.imagedir + '"  style="width:20px;height:20px;"></img>',
            ].join('');
        }
        var tableColumns = [
            {
                field: 'bannerid',
                title: 'ID',
                switchable: true
            }, {
                field: 'title',
                title: '标题',
                switchable: true
            },
            {
                field: 'imagedir',
                title: '图片',
                switchable: true,
                formatter: operateFormatterimage //自定义方法，添加操作按钮
            },
            {
                field: 'validflag',
                title: '状态',
                formatter: formatFormatter
            }, {
                field: 'operate',
                title: '操作',
                formatter: operateFormatter //自定义方法，添加操作按钮
            }
        ];
        $("#adtable").bootstrapTable('destroy'); //分页必须写
        $("#adtable").bootstrapTable({
            cache: false,
            pagination: true,
            sidePagination: "client",  //分页方式：client客户端分页，server服务端分页（*）
            pageNumber: 1,   //初始化加载第一页，默认第一页
            pageSize: 5,   //每页的记录行数（*）
            pageList: [5, 10], //可供选择的每页的行数
            columns: tableColumns,
            data: datas,
            queryParamsType: '',
            //得到查询的参数
            queryParams: function queryParams(params) {
                return params;
            },
            onLoadSuccess: function (data) {  //加载成功时执行
                alert("成功" + data)
            },
            onLoadError: function () {  //加载失败时执行
                alert("失败")
            }

        });
    }
    $.ajax({
        url: url,
        method: 'post',
        contentType: "application/x-www-form-urlencoded",
        success: function (res) {
            console.log(res.data)
            s(res.data)

        }
    })
}
function backad(e) {
    // 返回
    // 编辑
    divset = document.getElementsByClassName("search");
    for (var i = 0; i < divset.length; i++) {
        divset[i].style.display = "inline";
    };
    divsetup = document.getElementsByClassName("updateadimage");
    for (var i = 0; i < divset.length; i++) {
        divsetup[i].style.display = "none";
    };
    divsetedit = document.getElementsByClassName("editad");
    for (var i = 0; i < divset.length; i++) {
        divsetedit[i].style.display = "none";
    };
}
//  编辑按钮
function editad(bannerid, title, info, link, bannertypeid) {
    console.log(bannerid)
    console.log(title)
    console.log(info)
    //  赋值
    $("#editadtitle").val(title)
    $("#editdturl").val(link)
    $("#editadcontent").val(info)
    $("#editadimage").find("option[value='" + bannertypeid + "']").attr("selected", true);
    $("#bannerid").val(bannerid)
    // 编辑
    divset = document.getElementsByClassName("search");
    for (var i = 0; i < divset.length; i++) {
        divset[i].style.display = "none";
    };
    divsetup = document.getElementsByClassName("updateadimage");
    for (var i = 0; i < divset.length; i++) {
        divsetup[i].style.display = "none";
    };
    divsetedit = document.getElementsByClassName("editad");
    for (var i = 0; i < divset.length; i++) {
        divsetedit[i].style.display = "inline";
    };
}
//  编辑保存按钮
function editsaveaD(e) {
    // 获取值
    var bannertypeid = $("#editadimage").val()
    var bannerid = document.getElementById("bannerid").value
    var info = document.getElementById("editadcontent").value
    var link = document.getElementById("editdturl").value
    var title = document.getElementById("editadtitle").value
    var date = {
        bannerID: bannerid,
        bannerTypeID: bannertypeid,
        info: info,
        link: link,
        title: title
    }
    var datas = JSON.stringify(date)
    $.ajax({
        url: "http://192.168.0.112:8080/BannerManage/updateBannerInfo",
        method: "post",
        contentType: "application/json",
        data: datas,
        success: function (res) {
            console.log(res.data)
            // 返回
            // 编辑
            divset = document.getElementsByClassName("search");
            for (var i = 0; i < divset.length; i++) {
                divset[i].style.display = "inline";
                location.reload()
            };
            divsetup = document.getElementsByClassName("updateadimage");
            for (var i = 0; i < divset.length; i++) {
                divsetup[i].style.display = "none";
            };
            divsetedit = document.getElementsByClassName("editad");
            for (var i = 0; i < divset.length; i++) {
                divsetedit[i].style.display = "none";
            };
        }
    })
}
// 切换图片按钮
function updateadImage(bannerid) {
    console.log(bannerid)
    divset = document.getElementsByClassName("search");
    for (var i = 0; i < divset.length; i++) {
        divset[i].style.display = "none";
    };
    divsetup = document.getElementsByClassName("updateadimage");
    for (var i = 0; i < divset.length; i++) {
        divsetup[i].style.display = "inline";
    };
    $("#bannerid").val(bannerid)
}
// 切换图片保存
function updateadsaveImage(e) {
    divset = document.getElementsByClassName("search");
    for (var i = 0; i < divset.length; i++) {
        divset[i].style.display = "none";
    };
    divsetup = document.getElementsByClassName("updateadimage");
    for (var i = 0; i < divset.length; i++) {
        divsetup[i].style.display = "inline";
    };
  
    var formdata = new FormData()
    var bannerid = document.getElementById("bannerid").value
    var uploadfile = document.getElementById("exampleFormControlFile1").files[0]
    console.log(uploadfile.name)
    formdata.append("bannerID",bannerid)
    formdata.append("uploadFile",uploadfile)
    console.log(formdata)
    $.ajax({
        url: "http://192.168.0.112:8080/BannerManage/updateimg",
        method: "post",
        data: formdata,
        contentType: false,
        processData : false,
        async:false,
        success: function (res) {
            console.log(res.msg)
            // 返回
            // 编辑
            divset = document.getElementsByClassName("search");
            for (var i = 0; i < divset.length; i++) {
                divset[i].style.display = "inline";
                location.reload()
            };
            divsetup = document.getElementsByClassName("updateadimage");
            for (var i = 0; i < divset.length; i++) {
                divsetup[i].style.display = "none";
            };
            divsetedit = document.getElementsByClassName("editad");
            for (var i = 0; i < divset.length; i++) {
                divsetedit[i].style.display = "none";
            };
        }
    })
}
// 切换状态
function changeStata(e) {
    var Img = document.getElementById('img');
    $('#myModal').modal('hide');
    if (Img.getAttribute("src", 2) == '../../../images/success.png') {
        Img.setAttribute("src", "../../../images/fail.png");
        $('#modal-input').html('确认启用');
    } else {
        Img.setAttribute("src", '../../../images/success.png');
        //此处即为修改modal的标题
        $('#modal-input').html('确认停用');
    }
}
// 切换状态
// 当前发行版本状态
function statusChange(validflag,bannerid) {
    // 获取当前状态
    console.log(validflag);
    // 判断当前状态更改
    if(validflag === 1){
       $('#modal-input').html('确认停用');
       $('#myModal').modal('show');
        var adstatus=0
        console.log("dianji"+adstatus+bannerid);
        changeStata(adstatus,bannerid)
    }
    else{
       $('#modal-input').html('确认启用');
       $('#myModal').modal('show');
        var adstatus=1
        console.log("dianji"+adstatus+bannerid);
        changeStata(adstatus,bannerid)
    }
}
// 品牌状态更改按钮
// 切换状态
function changeStata(adstatus,bannerid) {
    $('#btn_ok').click(function(){
    $.ajax({
        url: 'http://192.168.0.112:8080/BannerManage/updateBannerStatus',
        method:'post',
        contentType:'application/x-www-form-urlencoded',
        data:{
            bannerID:bannerid,
            validFlag:adstatus
        },
         success: function (res) {
            console.log(res.msg);
            $('#myModal').modal('hide');
            location.reload();
         }
    })
})
}